from create_import import *
from table_list import *


def create_entity(table: str):
    _current = "entity"
    param = Params(table)
    _class_name = param.class_name_basic  # 当前类名

    _package_name = f"package {param.package_name_basic}.{_current};"  # 当前包名

    _java_output_path = f"{param.output_java_path_basic}/{_current}"  # java文件路径

    # print(f">>输出路径{_java_output_path}")
    if not os.path.exists(_java_output_path):
        os.makedirs(_java_output_path)

    _java_output_file = f"{_java_output_path}/{_class_name}.java"

    if os.path.exists(_java_output_file):
        os.remove(_java_output_file)
    _print_output = output(_java_output_file)  # print 的output

    def printf(val, end = "\n"):
        print(val, file=_print_output, end=end)

    demoblock = f"""{_package_name}
import java.io.Serializable;
import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName("{table}")
public class {_class_name} implements Serializable {'{'}

\tprivate static final long serialVersionUID = {random.randint(serialVersionUID_min_value, serialVersionUID_max_value)}L;

\t@TableId(value = "uuid", type = IdType.ASSIGN_UUID)
\tprivate String uuid;
    """
    printf(demoblock)  # import block
    with open(f"{local_dir}/{param.table}.txt", "r", encoding="utf-8") as f:
        lines = f.readlines()
        names = []
        for line in lines:
            names.append(line.rstrip().lower().split(","))

        cols = lines[1].rstrip().split(",")
        comments = lines[0].rstrip().split(",")
        types = lines[2].rstrip().split(",")

        for k in range(len(cols)):
            col = cols[k].lower()
            comment = comments[k]
            word = col.strip()
            tpye = types[k]
            printf(
                f"""
    /**
     * {comment}
     */"""
            )
            printf(f'\t@TableField("{"_".join(word.strip().split(" "))}")')
            w = uppercase_except_first(word)
            if "varchar" in tpye:
                printf(f"\tprivate String {w};")
            elif "int" in tpye:
                printf(f"\tprivate Integer {w};")
            elif "timestamp" in tpye:
                printf(f"\tprivate Date {w};")
        printf(
            """
    @TableField("data_time")
	private Date dataTime;
	
	@TableField("data_user")
	private String dataUser;
	
	@TableField("data_comment")
	private String dataComment;
            """
        )
    printf("}")
    print(f"{param.table}创建{_current} - 完成")


if __name__ == "__main__":
    for table in table_list:
        create_entity(table)
